package crs.web.validadores;


import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.validation.Errors;
import org.springframework.validation.Validator;

import crs.dispositivos.facade.DispositivosFacade;
import crs.dispositivos.tablebean.JsonJTableDispositivosBean;
import crs.users.facade.UserFacade;
 
@Component
public class DisValidator implements Validator {

	@Autowired
	private UserFacade userFacade;
	@Autowired
	private DispositivosFacade disFacade;
	
    //which objects can be validated by this validator
    @Override
    public boolean supports(Class<?> paramClass) {
        return JsonJTableDispositivosBean.class.equals(paramClass);
    }
 
    @Override
    public void validate(Object obj, Errors errors) {
         
        JsonJTableDispositivosBean dis = (JsonJTableDispositivosBean) obj;
        
        if (dis.getDispid()==0){
			if (disFacade.dispExist(dis.getDispositivoid())) {
				errors.rejectValue("dispositivoid", "message.disp.exist", new Object[]{dis.getDispositivoid()},"");
	        }
        }else{
        	if (disFacade.dispExist(dis.getDispositivoid(), dis.getDispid())) {
				errors.rejectValue("dispositivoid", "message.disp.exist", new Object[]{dis.getDispositivoid()},"");
	        }
	       
        }
		
		
			
		
         
        //ValidationUtils.rejectIfEmptyOrWhitespace(errors, "name", "name.required");
        //ValidationUtils.rejectIfEmptyOrWhitespace(errors, "role", "role.required");
        
        
    }
}